﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MedicalManagementSystem.Utils
{
    public class PaginatedList<T> : List<T>
    {
        private const int PAGINATED_WIDTH = 10;
        private const int PAGINATED_SIZE = 30;

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public int FirstPage { get; set; }
        public int LastPage { get; set; }
        public PaginatedList(IEnumerable<T> source, int pageIndex, int pageSize)
            : this(source, pageIndex, pageSize, PAGINATED_WIDTH)
        { }

        public PaginatedList(IEnumerable<T> source)
            : this(source, 0, PAGINATED_SIZE, PAGINATED_WIDTH)
        { }

        public PaginatedList(IEnumerable<T> source, int pageIndex, int pageSize = PAGINATED_SIZE, int paginatedWidth = PAGINATED_WIDTH)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            if (TotalPages <= paginatedWidth) 
            {
                FirstPage = 1;
                LastPage = TotalPages;
            }
            else if (PageIndex - paginatedWidth / 2 <= 0)
            {
                FirstPage = 1;
                LastPage = FirstPage + paginatedWidth;
            }
            else
            {
                if (PageIndex + paginatedWidth / 2 >= TotalPages)
                {
                    LastPage = TotalPages;
                    FirstPage = TotalPages - paginatedWidth;
                }
                else
                {
                    FirstPage = PageIndex - paginatedWidth / 2 + 1;
                    LastPage = PageIndex + paginatedWidth / 2 + 1;
                }
            }

            AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public PaginatedList(IEnumerable<T> source, T aimElement, int pageSize)
        {
            PageIndex = 0;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            while ((PageIndex < TotalPages) &&
                !source.Skip((PageIndex++) * PageSize).Take(PageSize)
                .ToArray().Contains(aimElement)) { }
            PageIndex--;
            if (PageIndex == TotalPages)
                PageIndex = 0;
            AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }
    }
}
